using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using FireStarter.CodeGeneration;
using FireStarterModeller.BO;
using NUnit.Framework;

namespace FireStarter.Test.CodeGeneration
{
    [TestFixture]
    public class TestSolutionFileManager : TestGeneratorBase
    {

        #region Utility Methods

        private string GetTestSolutionFileName()
        {
            return Path.Combine(TEST_PROJECTS_FOLDER, GetRandomString() + ".sln");
        }

        private static string GetExpectedContents()
        {
            string expectedContents = TemplateLoader.Sln(CodeTemplateEnum.CSharpVisualStudio2005);
            return expectedContents;
        }

        private SolutionFileManager CreateSolutionFileManager()
        {
            string solutionFileName = GetRootedPath(GetTestSolutionFileName());
            //CreateTestSolutionFile(solutionFileName);
            return new SolutionFileManager(solutionFileName, CodeTemplateEnum.CSharpVisualStudio2005);
        }

        #endregion // Utility Methods

        [Test]
        public void TestCreateEmptySolutionFile()
        {
            //---------------Set up test pack-------------------
            string solutionFileName = GetTestSolutionFileName();
            string solutionName = Path.GetFileNameWithoutExtension(solutionFileName);
            SolutionFileManager solutionFileManager = new SolutionFileManager(solutionFileName, CodeTemplateEnum.CSharpVisualStudio2005);
            string expectedContents = GetExpectedContents();
            //---------------Assert Precondition----------------
            Assert.IsFalse(File.Exists(solutionFileName));
            //---------------Execute Test ----------------------
            solutionFileManager.SaveSolutionFile();
            //---------------Test Result -----------------------
            Assert.IsTrue(File.Exists(solutionFileName));
            string contents = File.ReadAllText(solutionFileName);
            Assert.AreEqual(expectedContents, contents);
        }

        [Test]
        public void TestOpenExistingSolutionFile()
        {
            //---------------Set up test pack-------------------
            SolutionFileManager solutionFileManager = CreateSolutionFileManager();
            string solutionFileName = solutionFileManager.SolutionFileName;
            string projectFileName1 = GetRootedPath(GetRandomProjectFileName());
            ProjectFileManager projectFileManager = new ProjectFileManager(projectFileName1, OutputType.Library, CodeTemplateEnum.CSharpVisualStudio2005);
            projectFileManager.SaveProjectFile();
            solutionFileManager.AddProjectFile(projectFileName1);
            solutionFileManager.SaveSolutionFile();
            //---------------Assert Precondition----------------
            Assert.IsTrue(File.Exists(solutionFileName));
            //---------------Execute Test ----------------------
            solutionFileManager = new SolutionFileManager(solutionFileName, CodeTemplateEnum.CSharpVisualStudio2005);
            //---------------Test Result -----------------------
            Assert.IsTrue(File.Exists(solutionFileName));
            Assert.IsTrue(solutionFileManager.HasProjectFile(projectFileName1));
        }

        [Test]
        public void TestAddProjectFile()
        {
            //---------------Set up test pack-------------------
            SolutionFileManager solutionFileManager = CreateSolutionFileManager();
            string fileName = GetRootedPath(GetRandomProjectFileName());
            ProjectFileManager projectFileManager = new ProjectFileManager(fileName, OutputType.Library, CodeTemplateEnum.CSharpVisualStudio2005);
            projectFileManager.SaveProjectFile();
            //---------------Assert Precondition----------------
            Assert.IsFalse(solutionFileManager.HasProjectFile(fileName));
            //---------------Execute Test ----------------------
            solutionFileManager.AddProjectFile(fileName);
            //---------------Test Result -----------------------
            Assert.IsTrue(solutionFileManager.HasProjectFile(fileName));
        }

        [Test]
        public void TestAddProjectFile_TwoProjectFiles()
        {
            //---------------Set up test pack-------------------
            SolutionFileManager solutionFileManager = CreateSolutionFileManager();
            string projectFileName1 = GetRootedPath(GetRandomProjectFileName());
            ProjectFileManager projectFileManager = new ProjectFileManager(projectFileName1, OutputType.Library, CodeTemplateEnum.CSharpVisualStudio2005);
            projectFileManager.SaveProjectFile();
            string projectFileName2 = GetRootedPath(GetRandomProjectFileName());
            projectFileManager = new ProjectFileManager(projectFileName2, OutputType.Library, CodeTemplateEnum.CSharpVisualStudio2005);
            projectFileManager.SaveProjectFile();
            solutionFileManager.AddProjectFile(projectFileName1);
            //---------------Assert Precondition----------------
            Assert.IsTrue(solutionFileManager.HasProjectFile(projectFileName1));
            Assert.IsFalse(solutionFileManager.HasProjectFile(projectFileName2));
            //---------------Execute Test ----------------------
            solutionFileManager.AddProjectFile(projectFileName2);
            //---------------Test Result -----------------------
            Assert.IsTrue(solutionFileManager.HasProjectFile(projectFileName1));
            Assert.IsTrue(solutionFileManager.HasProjectFile(projectFileName2));
        }
    }
}
